//
//  CouponViewController.m
//  SXH
//
//  Created by Ryan on 16/6/14.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "CouponViewController.h"
#import "MJRefresh.h"
#import "CommonService.h"
#import "CouponTableViewCell.h"
@interface CouponViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    int page;
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
}
@property (weak, nonatomic) IBOutlet UIButton *noUseBtn;
@property (nonatomic,strong)NSMutableArray *listArray;
@end

@implementation CouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(62, 0, 0, 0));
    }];
    self.view.backgroundColor = HEXCOLOR(0xededed);
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    page = 1;
    [self addHeader];
    [self addFooter];
    if (self.needNoUseBtn) {
        [self.view addSubview:self.noUseBtn];
        self.noUseBtn.hidden = NO;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellId = @"CouponTableViewCell";
    CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {   
        cell = [[[NSBundle mainBundle]loadNibNamed:cellId owner:self options:nil]lastObject];
    }
    cell.model = [self.listArray objectAtIndex:indexPath.row];
    if (self.needNoUseBtn) {
        cell.buyRightNow.hidden = NO;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CouponModel *model = [self.listArray objectAtIndex:indexPath.row];
    if (_block) {
        self.block(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void) getActivityListData:(int)pageNo  RefreshView:(MJRefreshBaseView *)refreshView
{
    CommonService *service = [[CommonService alloc] init];
    NSString *strPageNo = [NSString stringWithFormat:@"%d", pageNo];
    NSMutableDictionary *parm = [[NSMutableDictionary alloc]initWithCapacity:2];
    [parm addEntriesFromDictionary:  @{@"limit":@"20",
                                      @"page":strPageNo,
                                      }];

    if (self.product_class_id)
    {
        [parm setValue:[NSString stringWithFormat:@"%@",self.product_class_id] forKey:@"product_class_id"];
    }
    if (self.product_id)
    {
        [parm setValue:self.product_id forKey:@"product_id"];
    }
    if (self.order_total_amount)
    {
        [parm setValue:self.order_total_amount forKey:@"order_total_amount"];
    }
    [service getNetWorkData:parm
                       Path:COUPONLIST
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      if (pageNo == 1)
                      {
                          self.listArray = [CouponModel arrayOfModelsFromDictionaries:[dic objectForKey:@"data"]];
                      }
                      else
                      {
                          [self.listArray addObjectsFromArray:[CouponModel arrayOfModelsFromDictionaries:[dic objectForKey:@"data"]]];
                      }

                      [self doneWithView:refreshView];
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      [refreshView endRefreshing];
                  }];
}

- (void)addFooter
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        ++page;
        [self getActivityListData:page RefreshView:refreshView];
        
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    _footer = footer;
}

- (void)addHeader
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        [self.listArray removeAllObjects];
        [self getActivityListData:1 RefreshView:refreshView];
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        NSLog(@"%@----刷新完毕", refreshView.class);
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    [header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    [self.tableView reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}
- (IBAction)doback:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)noUserBack:(id)sender {
    if (_block) {
        self.block(nil);
        [self.navigationController popViewControllerAnimated:YES];
    }
}



@end
